package com.luoxiang.socialandroid.activity

import android.os.Bundle
import android.widget.Toast
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.luoxiang.lib_common.bean.ResponseDTO
import com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA
import com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN
import com.luoxiang.socialandroid.R
import com.luoxiang.socialandroid.base.BaseActivity
import com.luoxiang.socialandroid.bean.NumberBean
import com.luoxiang.socialandroid.common.Constants.MmkvConst.KEY
import com.luoxiang.socialandroid.databinding.ActivityApplyTradeUnionBinding
import com.luoxiang.socialandroid.utils.AESUtil
import com.luoxiang.socialandroid.utils.MmkvUtil
import com.luoxiang.socialandroid.utils.NetUtils
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

class ApplyTradeUnionActivity : BaseActivity() {
    private lateinit var mViewBinding: ActivityApplyTradeUnionBinding

    override fun initLayout(savedInstanceState: Bundle?) {
        mViewBinding = ActivityApplyTradeUnionBinding.inflate(layoutInflater)
        setContentView(mViewBinding.root)
        setStatusBarTranslucent()
    }

    override fun initView() {

    }

    override fun initListener() {
        mViewBinding.imageBack.setOnClickListener {
            finish()
        }
        mViewBinding.textApply.setOnClickListener {
            saveGuildAudit()
        }
    }

    private fun saveGuildAudit() {
        val applicantName = mViewBinding.editApplicantName.text.toString().ifBlank {
            Toast.makeText(this, "请输入申请人名称", Toast.LENGTH_SHORT).show()
            return
        }
        val contact = mViewBinding.editContact.text.toString().ifBlank {
            Toast.makeText(this, "请输入联系方式", Toast.LENGTH_SHORT).show()
            return
        }
        val channelName = mViewBinding.editChannelName.text.toString().ifBlank {
            Toast.makeText(this, "工会名称", Toast.LENGTH_SHORT).show()
            return
        }
        val hostNum = mViewBinding.editHostNum.text.toString().ifBlank {
            Toast.makeText(this, "请输入主播人数", Toast.LENGTH_SHORT).show()
            return
        }.let {
            it.toIntOrNull() ?: return
        }
        NetUtils.getInstance().apiService.saveGuildAudit(
            EXTRA,
            MmkvUtil.decodeString(TOKEN),
            applicantName,
            contact,
            channelName,
            hostNum
        ).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<NumberBean> {
                override fun onSubscribe(d: Disposable?) {}

                override fun onError(e: Throwable?) {}

                override fun onComplete() {}

                override fun onNext(value: NumberBean?) {
                    val data: String? = value?.data
                    var decryptStr: String? = null
                    try {
                        decryptStr = AESUtil.aesDecryptString(data, KEY)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                    val responseDTO = Gson().fromJson<ResponseDTO>(
                        decryptStr, object : TypeToken<ResponseDTO>() {}.type
                    )
                    if (responseDTO.code == 0) {
                        Toast.makeText(this@ApplyTradeUnionActivity, "提交申请成功", Toast.LENGTH_SHORT)
                            .show()
                        finish()
                        return
                    }
                    Toast.makeText(
                        this@ApplyTradeUnionActivity,
                        responseDTO.msg,
                        Toast.LENGTH_SHORT
                    ).show()
                }
            })
    }
}